package com.zxlfysj.stack;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author yangshujing
 * @create 2020-08-16 22:44
 */
public class PolandNotation {
    public static void main(String[] args) {
        //测试的表达式 (3+4)*5-6 => 3 4 + 5 * 6 -
        String suffixExpression = "3 4 + 5 * 6 - "; //这里空格隔开

        //放入到一个ArrayList,方便后面遍历
        List<String> rpnList = getListString(suffixExpression);
        System.out.println(rpnList);
        System.out.println(calculate(rpnList));

        //中缀表达式转成后缀表达式
        String expression = "1+((2+3)*4)-5";
        List<String> infixExpressionList = toInfixExpression(expression);
        System.out.println(infixExpressionList);

        System.out.println(parseSuffixExpressionList(infixExpressionList));

    }

    //将中缀表达式list转成后缀表达式list
    public static List<String> parseSuffixExpressionList(List<String> list) {
        Stack<String> s1 = new Stack<>();
        List<String> s2 = new ArrayList<>();
        for(String item : list) {
            if(item.matches("\\d+")) { //遇到数字直接入list
                s2.add(item);
            } else if (item.equals("(")) {  //遇到左括号直接入栈
                s1.push(item);
            } else if (item.equals(")")) {
                //如果是右括号，则依次弹出s1栈的运算符，并添加到s2，直到遇到左括号，并将左括号弹出丢弃
                //即消掉左右括号
                while (!s1.peek().equals("(")) {
                    s2.add(s1.pop());
                }
                s1.pop();//丢弃左括号
            } else {
                //如果当前运算符优先级小于等于s1栈顶的运算符，就将s1栈顶的运算符弹出加入到s2,循环执行
                while (s1.size() != 0 && Operation.getValue(s1.peek()) >= Operation.getValue(item)) {
                    s2.add(s1.pop());
                }
                s1.push(item);
            }

        }
        //将s1剩余的运算符依次弹出加入s2
        while (s1.size() != 0) {
            s2.add(s1.pop());
        }
        return s2;
    }

    //将中缀表达式转成对应的list
    public static List<String> toInfixExpression(String s) {
        List<String> list = new ArrayList<>();
        int i = 0;
        String str;
        char c;
        do {
            //如果是非数字，直接添加到list
            if((c=s.charAt(i)) < 48 || (c=s.charAt(i)) > 57) {
                list.add("" + c);
                i++;
            } else {
                str = "";
                while (i < s.length() && (c = s.charAt(i)) >= 48 && (c=s.charAt(i)) <= 57 ) {
                    str += c;
                    i++;
                }
                list.add(str);

            }
        } while (i < s.length());
        return list;

    }

    //处理表达式，放入数组中
    public static List<String> getListString(String suffixExpression) {
        String[] split = suffixExpression.split(" ");
        List<String> list = new ArrayList<String>();
        for(String ele : split) {
            list.add(ele);
        }
        return list;

    }

    //进行计算
    public static int calculate(List<String> list) {
        Stack<String> stack = new Stack<String>();
        for(String item : list) {
            if(item.matches("\\d+")) { //匹配到数字就入栈
                stack.push(item);
            } else {
                int num2 = Integer.parseInt(stack.pop());
                int num1 = Integer.parseInt(stack.pop());
                int res = 0;
                if(item.equals("+")) {
                    res = num1 + num2;
                } else if (item.equals("-")) {
                    res = num1 - num2;
                } else if (item.equals("*")) {
                    res = num1 * num2;
                } else if (item.equals("/")) {
                    res = num1 / num2;
                } else {
                    throw new RuntimeException("运算符有误");
                }
                stack.push("" + res); //转成字符串再入栈
            }
        }
        return Integer.parseInt(stack.pop());
    }
}
//返回运算符对应的优先级
class Operation {
    private static int ADD = 1;
    private static int SUB = 1;
    private static int MUL = 2;
    private static int DIV = 2;

    public static int getValue(String operation) {
        int result = 0;
        switch (operation) {
            case "+":
                result = ADD;
                break;
            case "-":
                result = SUB;
                break;
            case "*":
                result = MUL;
                break;
            case "/":
                result = DIV;
                break;
                default:
                    System.out.println("不存在此运算符");
                    break;
        }
        return result;
    }

}
